package tw.twandroidproj.app.ui.resume;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import tw.twandroidproj.app.R;
import tw.twandroidproj.app.model.Resume;

public class ResumeAdapter extends RecyclerView.Adapter<ResumeAdapter.ViewHolder> {

  private Context resumeContext;
  private List<Resume> resumeData;

  public ResumeAdapter(Context context,List<Resume> resumelist) {
    resumeContext = context;
    resumeData = new ArrayList<Resume>();
    resumeData = resumelist;
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(resumeContext).inflate(R.layout.resume_item, parent, false);
    return new ViewHolder(v);
  }

  @Override
  public void onBindViewHolder(ViewHolder holder, final int position) {

    holder.resume_Name.setText(resumeData.get(position).getName());
    holder.resume_updateTime.setText(resumeData.get(position).getUpdated_at());

    holder.resume_View.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent resume_view = new Intent(resumeContext, Resume_view.class);
        resumeContext.startActivity(resume_view);
      }
    });

    holder.resume_Edit.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent resume_edit=new Intent(resumeContext,Resume_edit.class);
        resumeContext.startActivity(resume_edit);
      }
    });

    holder.resume_Delete.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        new AlertDialog.Builder(resumeContext).
                setTitle("确定要删除 "+resumeData.get(position).getName()+" 吗？").
                setPositiveButton("确定",new DialogInterface.OnClickListener(){
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    remove(position);
                  }
                }).
                setNegativeButton("取消",null).
                create().
                show();
      }
    });
  }

  @Override
  public int getItemCount() {
    return resumeData.size();
  }

  public void remove(int position) {
    resumeData.remove(position);
    notifyItemRemoved(position);
  }


    class ViewHolder extends RecyclerView.ViewHolder{
     TextView resume_Name, resume_updateTime,resume_Edit, resume_Delete;
     ImageView resume_View;

    public ViewHolder(View itemView) {
      super(itemView);
      resume_Name=(TextView)itemView.findViewById(R.id.resume_Name);
      resume_View  = (ImageView) itemView.findViewById(R.id.resume_View);
      resume_updateTime=(TextView)itemView.findViewById(R.id.resume_updateTime);
      resume_Edit=(TextView)itemView.findViewById(R.id.resume_Edit);
      resume_Delete=(TextView)itemView.findViewById(R.id.resume_Delete);
    }
  }

}
